﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WOWSharp.Community;
using WOWSharp.Community.ObjectModel;
using System.Net;

namespace AuctionHouseDataLibrary
{
    public class AuctionHouseDataProcessor
    {
        public const string Alliance = "Alliance";
        public const string Horde = "Horde";
        public const string Neutral = "Neutral";

        public AuctionHouseDataProcessor(bool alliance, bool horde, bool neutral, string realm, BackgroundDownloadThread backgroundDownloadThread)
        {
            _alliance = alliance;
            _horde = horde;
            _neutral = neutral;
            _realm = realm;
            _backgroundDownloadThread = backgroundDownloadThread;
        }

        private bool _alliance;
        private bool _horde;
        private bool _neutral;
        private string _realm;
        private ApiClient _client = new ApiClient(Region.US, "en-AU");
        private AuctionDump _auctionDump;
        private ItemDatabaseContext _context;
        private BackgroundDownloadThread _backgroundDownloadThread;

        public bool Process()
        {
            try
            {
                _auctionDump = _client.GetAuctionDump(_realm);
                UpdateAuctionDetails();
                return true;
            }
            catch (WebException ex)
            {
                // Unable to download data
                Globals.Logger.Error("Web exception when trying to download auction data - " + ex.Message);
            }
            catch (WoWAPIException ex)
            {
                // Unable to download data
                Globals.Logger.Error("WoW API exception when trying to download auction data - " + ex.Message);
            }
            return false;
        }

        private void UpdateAuctionDetails()
        {
            using (_context = new ItemDatabaseContext())
            {
                ScanEvent scanEvent =
                    _context.ScanEvents.SingleOrDefault(p => p.LastModDateTime == _auctionDump.LastModifiedUtc);
                if (scanEvent == null)
                {
                    AddNewScanEvent();
                    if (_alliance)
                    {
                        QueueAddNewAuctions(_auctionDump.Alliance, Alliance);
                    }
                    if (_horde)
                    {
                        QueueAddNewAuctions(_auctionDump.Horde, Horde);
                    }
                    if (_neutral)
                    {
                        QueueAddNewAuctions(_auctionDump.Neutral, Neutral);
                    }
                }
                else
                {
                    Globals.Logger.InfoFormat("Scan event already exists, skipping data collation {0}", scanEvent);
                }
            }
        }
        
        private void QueueAddNewAuctions(AuctionHouse auctionHouse, string auctionHouseString)
        {
            ScanEvent currentScanEvent = _context.ScanEvents.SingleOrDefault(p => p.LastModDateTime == _auctionDump.LastModifiedUtc);
            var addList = auctionHouse.Auctions.Select(auction => new Auction
                {
                    AuctionId = auction.AuctionId,
                    BuyoutValue = auction.BuyoutValue,
                    ItemId = auction.ItemId,
                    OwnerName = auction.OwnerName,
                    Quantity = auction.Quantity,
                    CurrentBidValue = auction.CurrentBidValue,
                    AuctionHouse = auctionHouseString
                }).ToList();
            _backgroundDownloadThread.QueueAuctionUpdateList(addList, currentScanEvent.Id);
            Globals.Logger.InfoFormat("Queue {0} auctions for realm: {1} auction house: {2}", addList.Count, _realm, auctionHouseString);
        }

        private void AddNewScanEvent()
        {
            ScanEvent newScanEvent = new ScanEvent()
                {
                    Alliance = _alliance,
                    Horde = _horde,
                    Netural = _neutral,
                    LastModDateTime = _auctionDump.LastModifiedUtc,
                    Realm = _realm
                };
            _context.ScanEvents.Add(newScanEvent);
            _context.SaveChanges();
            Globals.Logger.InfoFormat("New scan event added {0}", newScanEvent);
        }
    }
}